package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupAndCheckItemRelation;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

   @Autowired
   private CheckGroupMapper checkGroupMapper;


   @Override
   public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
      //1.添加检查组数据
      checkGroupMapper.add(checkGroup);

      //2.获取检查组id
      Integer checkGroupId = checkGroup.getId();
      System.out.println(checkGroupId);//测试

      //3.遍历数组,添加组和项的关系
      /*for (Integer checkitemId : checkitemIds) {
         checkGroupMapper.addGroupAndItemRel(checkGroupId,checkitemId);
      }*/

      //3.改进批量插入
      //3.1封装集合
      /*ArrayList<CheckGroupAndCheckItemRelation> list = new ArrayList<>();
      for (Integer checkitemId : checkitemIds) {
         //创建对象
         CheckGroupAndCheckItemRelation relation = new CheckGroupAndCheckItemRelation(checkGroupId, checkitemId);
         //添加对象到集合
         list.add(relation);
      }*/
      //调用批量插入方法
      batchInsert(checkitemIds, checkGroupId);

   }

   //批量插入方法
   private void batchInsert(Integer[] checkitemIds, Integer checkGroupId) {
      //3.添加组和项关系：使用map封装组id和项id方式，不使用自定义类
      if (checkitemIds != null && checkitemIds.length > 0) {
         ArrayList<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
         for (Integer checkitemId : checkitemIds) {
            //创建map，封装组id和项id
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("checkGroupId", checkGroupId);
            map.put("checkItemId",checkitemId);
            //添加map到list中
            list.add(map);

         }
         //调用批量组合项的关系方法
         checkGroupMapper.addGroupAndItemRel2(list);
      }
   }

   @Override
   public PageResult findPage(QueryPageBean queryPageBean) {
      //1.获取参数
      Integer currentPage = queryPageBean.getCurrentPage();
      Integer pageSize = queryPageBean.getPageSize();
      String queryString = queryPageBean.getQueryString();

      //2.使用分页助手
      PageHelper.startPage(currentPage,pageSize);

      //3.调用mapper的findByCondition方法
      Page<CheckGroup> page = checkGroupMapper.findByCondition(queryString);

      //4.返回分页结果
      return new PageResult(page.getTotal(),page.getResult());
   }

   @Override
   public CheckGroup findById(Integer id) {
      CheckGroup checkGroup = checkGroupMapper.findById(id);
      return checkGroup;

   }

   @Override
   public List<Integer> findItemIdsByGroupId(Integer groupId) {
      List<Integer> checkItemIds = checkGroupMapper.findItemIdsByGroupId(groupId);
      return checkItemIds;
   }

   //修改检查组数据
   public void update(Integer[] checkitemIds, CheckGroup checkGroup) {
      //1.修改检查组数据
      checkGroupMapper.update(checkGroup);

      //2.通过组id删除组项间的关系
      Integer groupId = checkGroup.getId();
      checkGroupMapper.deleteByGroupId(groupId);

      //3.添加新的组项间的关系
      //3.1封装集合
      /*if (checkitemIds != null && checkitemIds.length > 0) {
         ArrayList<CheckGroupAndCheckItemRelation> list = new ArrayList<>();
         for (Integer checkitemId : checkitemIds) {
            //创建对象
            CheckGroupAndCheckItemRelation relation = new CheckGroupAndCheckItemRelation(groupId, checkitemId);
            //添加对象到集合
            list.add(relation);
         }
         checkGroupMapper.addGroupAndItemRel2(list);
      }*/
      //3.调用批量插入方法
      batchInsert(checkitemIds, groupId);
   }

   //查询所有检查组
   public List<CheckGroup> findAll() {
      List<CheckGroup> list = checkGroupMapper.findAll();
      return list;
   }
}
